target_include_directories(zeno PRIVATE .)

find_package(Eigen3 REQUIRED)
target_link_libraries(zeno PRIVATE Eigen3::Eigen)

option(ZENO_CGMESH_WITH_VOROXX "Build with Voro++ nodes" ON)
if (ZENO_CGMESH_WITH_VOROXX)
    target_sources(zeno PRIVATE VoronoiFracture.cpp)
    if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libigl/voro/CMakeLists.txt)
        message(FATAL_ERROR "libigl/voro submodule not found! Please run: git submodule update --init --recursive")
    endif()
    add_subdirectory(libigl/voro)
    target_link_libraries(zeno PRIVATE voro++)
endif()

option(ZENO_CGMESH_WITH_MESHFIX "Build with MeshFix nodes" ON)
if (ZENO_CGMESH_WITH_MESHFIX)
    target_sources(zeno PRIVATE PrimitiveMeshingFix.cpp)
    #set(BUILD_SHARED_LIBS OFF)
    if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libigl/meshfix/CMakeLists.txt)
        message(FATAL_ERROR "libigl/meshfix submodule not found! Please run: git submodule update --init --recursive")
    endif()
    add_subdirectory(libigl/meshfix)
    target_include_directories(zeno PRIVATE libigl/meshfix/JMeshExt-1.0alpha_src/include libigl/meshfix/JMeshExt-1.0alpha_src/JMeshLib-1.2/include)
    #add_definitions(-DUSE_FORTRAN_BLAS)
    #if(APPLE)
      #set(CMAKE_CXX_LINK_FLAGS "-framework Accelerate")
      #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
    #endif()
    target_link_libraries(zeno PRIVATE meshfix jmesh jmeshext nl superlu)
    if (UNIX)
        target_link_libraries(zeno PRIVATE blas lapack)
    else()
        find_package(OpenBLAS CONFIG REQUIRED)
        find_package(LAPACK CONFIG REQUIRED)
        target_link_libraries(zeno PRIVATE OpenBLAS::OpenBLAS)
        target_link_libraries(zeno PRIVATE ${LAPACK_LIBRARIES})
    endif()
endif()

option(ZENO_CGMESH_WITH_LIBIGL "Build with LIBIGL nodes" ON)
if (ZENO_CGMESH_WITH_LIBIGL)
    #set(CMAKE_LINK_WHAT_YOU_USE TRUE)
    set(LIBIGL_USE_STATIC_LIBRARY     OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_CGAL              ON  CACHE BOOL "EMBED")
    set(LIBIGL_WITH_TETGEN            ON  CACHE BOOL "EMBED")
    set(LIBIGL_WITH_COMISO            OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_EMBREE            OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_OPENGL            OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_OPENGL_GLFW       OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_OPENGL_GLFW_IMGUI OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_PNG               OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_TETGEN            OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_TRIANGLE          OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_PREDICATES        OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_XML               OFF CACHE BOOL "EMBED")
    set(LIBIGL_WITH_PYTHON            OFF CACHE BOOL "EMBED")
    set(CGAL_DO_NOT_WARN_ABOUT_CMAKE_BUILD_TYPE TRUE CACHE BOOL "EMBED" FORCE)
    if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libigl/CMakeLists.txt)
        message(FATAL_ERROR "libigl submodule not found! Please run: git submodule update --init --recursive")
    endif()
    add_subdirectory(libigl)
    if(NOT TARGET igl::core)
        find_package(libigl REQUIRED)
    endif()
    target_link_libraries(zeno PRIVATE igl::core)
    if (LIBIGL_WITH_CGAL)
        find_package(Boost COMPONENTS thread)
        if (TARGET Boost::thread)
            target_link_libraries(zeno PRIVATE Boost::thread)
        endif()
        target_link_libraries(zeno PRIVATE igl::cgal)
        target_sources(zeno PRIVATE igl_sink.cpp igl_dummy.cpp PrimitiveBooleanOp.cpp)
    endif()
endif()

find_package(OpenMP)
if (TARGET OpenMP::OpenMP_CXX)
    message("found package: OpenMP::OpenMP_CXX")
    target_link_libraries(zeno PRIVATE OpenMP::OpenMP_CXX)
endif()

if(TARGET igl_cgal)
    target_link_libraries(zeno PRIVATE igl_cgal)
endif()
